/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.service.paste;

import cn.ac.nercis.pes.common.constant.evaluation.EvaluationModule;
import cn.ac.nercis.pes.common.constant.project.FinishState;
import cn.ac.nercis.pes.common.constant.project.SystemState;
import cn.ac.nercis.pes.common.utils.DataUtils;
import cn.ac.nercis.pes.model.dto.common.DataCopy;
import cn.ac.nercis.pes.model.dto.common.DataPaste;
import cn.ac.nercis.pes.model.dto.common.Result;
import cn.ac.nercis.pes.model.dto.evaluation.content.proof.*;
import cn.ac.nercis.pes.model.dto.evaluation.prepare.EvaluationStageDTO;
import cn.ac.nercis.pes.model.dto.evaluation.proof.EvaluatorQualificationDTO;
import cn.ac.nercis.pes.model.dto.project.SystemProcessDTO;
import cn.ac.nercis.pes.service.evaluation.EvaluationProcessService;
import cn.ac.nercis.pes.service.evaluation.ProofService;
import cn.ac.nercis.pes.service.evaluation.UnitEvaluationService;
import cn.ac.nercis.pes.service.project.SystemProcessService;
import cn.ac.nercis.pes.service.utils.FileManage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 密评证明记录阶段相关数据粘贴服务类
 *
 * @author limy
 * @version 1.0.0
 */
@Slf4j
@Service
public record ProofPasteService(UnitEvaluationService unitEvaluationService,
                                SystemProcessService systemProcessService,
                                ProofService proofService,
                                EvaluationProcessService evaluationProcessService) {
    @Autowired
    public ProofPasteService {
    }


    public Result<String> proofDataPaste(DataPaste dataPaste, DataCopy dataCopy) {
        if (StringUtils.equals(dataCopy.getCopySystemId(), dataPaste.getSystemId()) && StringUtils.equals(dataCopy.getName(), dataPaste.getName())) {
            return Result.success();
        }
        var module = EvaluationModule.fromNameByProof(dataCopy.getName());
        return switch (module) {
            case PROOF_DELEGATE ->
                    pasteProofDelegate(dataCopy.getCopySystemId(), dataPaste.getSystemId(), dataCopy.getName());
            case PROOF_ACTIVITY ->
                    pasteProofActivity(dataCopy.getCopySystemId(), dataPaste.getSystemId(), dataCopy.getName());
            case PROOF_QUALITY ->
                    pasteProofQuality(dataCopy.getCopySystemId(), dataPaste.getSystemId(), dataCopy.getName());
            case PROOF_PERSON ->
                    pasteProofPerson(dataCopy.getCopySystemId(), dataPaste.getSystemId(), dataCopy.getName());
            case PROOF_GRADING ->
                    pasteProofGrading(dataCopy.getCopySystemId(), dataPaste.getSystemId(), dataCopy.getName());
            default -> Result.failed("不支持的数据复制操作！");
        };
    }

    private void pasteProofSystemProcess(String copySystemId, String currentSystemId, String name) {
        List<SystemProcessDTO> processDTOS = systemProcessService.findSystemProcess(copySystemId, SystemState.PROOF);
        EvaluationModule module = EvaluationModule.fromNameByProof(name);
        var state = processDTOS.stream().filter(p -> StringUtils.equals(p.getName(), name))
                .map(SystemProcessDTO::getFinishState)
                .findFirst().orElse(FinishState.NONE);
        systemProcessService.setSystemProcess(currentSystemId, module, state);
    }

    private Result<String> pasteProofDelegate(String copySystemId, String currentSystemId, String name) {
        DelegateDTO copyDelegateDTO = proofService.findDelegate(copySystemId);
        DelegateDTO pasteDelegateDTO = proofService.findDelegate(currentSystemId);
        //删除当前系统旧文件
        deleteDelegateFile(currentSystemId, pasteDelegateDTO);
        //拷贝新文件
        pasteDelegateFile(copySystemId, currentSystemId, copyDelegateDTO);
        var result = proofService.saveDelegate(currentSystemId, copyDelegateDTO);
        if (result.isSuccess()) {
            pasteProofSystemProcess(copySystemId, currentSystemId, name);
            return Result.success();
        } else {
            return Result.failed("");
        }

    }

    private void deleteDelegateFile(String currentSystemId, DelegateDTO pasteDelegateDTO) {
        if (StringUtils.isNotBlank(pasteDelegateDTO.getServiceFile())) {
            File deleteFile = FileUtils.getFile(FileManage.getDelegateDir(currentSystemId), pasteDelegateDTO.getServiceFile());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteDelegateDTO.getTollFile())) {
            File deleteFile = FileUtils.getFile(FileManage.getDelegateDir(currentSystemId), pasteDelegateDTO.getTollFile());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteDelegateDTO.getSignFile())) {
            File deleteFile = FileUtils.getFile(FileManage.getDelegateDir(currentSystemId), pasteDelegateDTO.getSignFile());
            deleteFile.delete();
        }
    }

    private void pasteDelegateFile(String copySystemId, String currentSystemId, DelegateDTO copyDelegateDTO) {
        try {
            //添加新文件
            if (StringUtils.isNotBlank(copyDelegateDTO.getServiceFile())) {
                File file = FileUtils.getFile(FileManage.getDelegateDir(copySystemId), copyDelegateDTO.getServiceFile());
                File saveFile = FileUtils.getFile(FileManage.getDelegateDir(currentSystemId), copyDelegateDTO.getServiceFile());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyDelegateDTO.getTollFile())) {
                File file = FileUtils.getFile(FileManage.getDelegateDir(copySystemId), copyDelegateDTO.getTollFile());
                File saveFile = FileUtils.getFile(FileManage.getDelegateDir(currentSystemId), copyDelegateDTO.getTollFile());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyDelegateDTO.getSignFile())) {
                File file = FileUtils.getFile(FileManage.getDelegateDir(copySystemId), copyDelegateDTO.getSignFile());
                File saveFile = FileUtils.getFile(FileManage.getDelegateDir(currentSystemId), copyDelegateDTO.getSignFile());
                FileManage.copyFile(file, saveFile);
            }

        } catch (IOException ex) {
            log.error("粘贴密评委托证明相关数据出错!", ex);
        }
    }

    private Result<String> pasteProofActivity(String copySystemId, String currentSystemId, String name) {
        ActivityDTO copyActivityDTO = proofService.findActivity(copySystemId);
        ActivityDTO pasteActivityDTO = proofService.findActivity(currentSystemId);
        var stage = evaluationProcessService.getEvaluationStage(currentSystemId);
        if (Objects.nonNull(stage)) {
            if (Objects.nonNull(copyActivityDTO.getStartDateTicket()) && Objects.nonNull(copyActivityDTO.getEndDateTicket())) {
                if (!validDateRange(copyActivityDTO.getStartDateTicket(), copyActivityDTO.getEndDateTicket(), stage)) {
                    copyActivityDTO.setStartDateTicket(pasteActivityDTO.getStartDateTicket());
                    copyActivityDTO.setEndDateTicket(pasteActivityDTO.getEndDateTicket());
                }
            }
            if (Objects.nonNull(copyActivityDTO.getStartDateRecord()) && Objects.nonNull(copyActivityDTO.getEndDateRecord())) {
                if (!validDateRange(copyActivityDTO.getStartDateRecord(), copyActivityDTO.getEndDateRecord(), stage)) {
                    copyActivityDTO.setStartDateRecord(pasteActivityDTO.getStartDateRecord());
                    copyActivityDTO.setEndDateRecord(pasteActivityDTO.getEndDateRecord());
                }
            }

        }
        deleteActivityFile(currentSystemId, pasteActivityDTO);
        pasteActivityFile(copySystemId, currentSystemId, copyActivityDTO);
        var result = proofService.saveActivity(currentSystemId, copyActivityDTO);
        if (result.isSuccess()) {
            pasteProofSystemProcess(copySystemId, currentSystemId, name);
            return Result.success();
        } else {
            return Result.failed("");
        }
    }

    private boolean validDateRange(LocalDate start, LocalDate end, EvaluationStageDTO stage) {
        var evalStartDate = stage.getEvaluationStartDate();
        var evalEndDate = stage.getEvaluationEndDate();
        if (Objects.isNull(evalStartDate) || Objects.isNull(evalEndDate)) {
            return true;
        }
        long between = DataUtils.between(start, end, ChronoUnit.DAYS);
        if (between < 1 || !DataUtils.range(start, evalStartDate, evalEndDate) || !DataUtils.range(end, evalStartDate, evalEndDate)) {
            return false;
        }
        return true;
    }

    private void deleteActivityFile(String currentSystemId, ActivityDTO pasteActivityDTO) {
//        旅行住宿等票证
        if (StringUtils.isNotBlank(pasteActivityDTO.getTravelFileTicket())) {
            File deleteFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), pasteActivityDTO.getTravelFileTicket());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteActivityDTO.getStayFileTicket())) {
            File deleteFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), pasteActivityDTO.getStayFileTicket());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteActivityDTO.getStayInvoiceFileTicket())) {
            File deleteFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), pasteActivityDTO.getStayInvoiceFileTicket());
            deleteFile.delete();
        }
//        进场记录
        if (StringUtils.isNotBlank(pasteActivityDTO.getSignFormFileRecord())) {
            File deleteFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), pasteActivityDTO.getSignFormFileRecord());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteActivityDTO.getScenePhotoFileRecord())) {
            File deleteFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), pasteActivityDTO.getScenePhotoFileRecord());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteActivityDTO.getDockingFileRecord())) {
            File deleteFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), pasteActivityDTO.getDockingFileRecord());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteActivityDTO.getInOutFileRecord())) {
            File deleteFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), pasteActivityDTO.getInOutFileRecord());
            deleteFile.delete();
        }
    }

    private void pasteActivityFile(String copySystemId, String currentSystemId, ActivityDTO copyActivityDTO) {
        try {
//          旅行住宿等票证
            if (StringUtils.isNotBlank(copyActivityDTO.getTravelFileTicket())) {
                File file = FileUtils.getFile(FileManage.getActivityDir(copySystemId), copyActivityDTO.getTravelFileTicket());
                File saveFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), copyActivityDTO.getTravelFileTicket());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyActivityDTO.getStayFileTicket())) {
                File file = FileUtils.getFile(FileManage.getActivityDir(copySystemId), copyActivityDTO.getStayFileTicket());
                File saveFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), copyActivityDTO.getStayFileTicket());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyActivityDTO.getStayInvoiceFileTicket())) {
                File file = FileUtils.getFile(FileManage.getActivityDir(copySystemId), copyActivityDTO.getStayInvoiceFileTicket());
                File saveFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), copyActivityDTO.getStayInvoiceFileTicket());
                FileManage.copyFile(file, saveFile);
            }
            //        进场记录
            if (StringUtils.isNotBlank(copyActivityDTO.getSignFormFileRecord())) {
                File file = FileUtils.getFile(FileManage.getActivityDir(copySystemId), copyActivityDTO.getSignFormFileRecord());
                File saveFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), copyActivityDTO.getSignFormFileRecord());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyActivityDTO.getScenePhotoFileRecord())) {
                File file = FileUtils.getFile(FileManage.getActivityDir(copySystemId), copyActivityDTO.getScenePhotoFileRecord());
                File saveFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), copyActivityDTO.getScenePhotoFileRecord());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyActivityDTO.getDockingFileRecord())) {
                File file = FileUtils.getFile(FileManage.getActivityDir(copySystemId), copyActivityDTO.getDockingFileRecord());
                File saveFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), copyActivityDTO.getDockingFileRecord());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyActivityDTO.getInOutFileRecord())) {
                File file = FileUtils.getFile(FileManage.getActivityDir(copySystemId), copyActivityDTO.getInOutFileRecord());
                File saveFile = FileUtils.getFile(FileManage.getActivityDir(currentSystemId), copyActivityDTO.getInOutFileRecord());
                FileManage.copyFile(file, saveFile);
            }
        } catch (IOException ex) {
            log.error("粘贴密评活动证明相关数据出错!", ex);
        }
    }

    private Result<String> pasteProofQuality(String copySystemId, String currentSystemId, String name) {
        QualityDTO copyQualityDTO = proofService.findQuality(copySystemId);
        QualityDTO pasteQualityDTO = proofService.findQuality(currentSystemId);
        deleteQualityFile(currentSystemId, pasteQualityDTO);
        pasteQualityFile(copySystemId, currentSystemId, copyQualityDTO);
        var result = proofService.saveQuality(currentSystemId, copyQualityDTO);
        if (result.isSuccess()) {
            pasteProofSystemProcess(copySystemId, currentSystemId, name);
            return Result.success();
        } else {
            return Result.failed("");
        }
    }

    private void deleteQualityFile(String currentSystemId, QualityDTO pasteQualityDTO) {
//        授权及告知
        if (StringUtils.isNotBlank(pasteQualityDTO.getAuthFileQuality())) {
            File deleteFile = FileUtils.getFile(FileManage.getQualityDir(currentSystemId), pasteQualityDTO.getAuthFileQuality());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteQualityDTO.getNoticeFileQuality())) {
            File deleteFile = FileUtils.getFile(FileManage.getQualityDir(currentSystemId), pasteQualityDTO.getNoticeFileQuality());
            deleteFile.delete();
        }
//        方案评审
        if (StringUtils.isNotBlank(pasteQualityDTO.getReviewRecordFileScheme())) {
            File deleteFile = FileUtils.getFile(FileManage.getQualityDir(currentSystemId), pasteQualityDTO.getReviewRecordFileScheme());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteQualityDTO.getConfirmRecordFileScheme())) {
            File deleteFile = FileUtils.getFile(FileManage.getQualityDir(currentSystemId), pasteQualityDTO.getConfirmRecordFileScheme());
            deleteFile.delete();
        }
//        报告评审
        if (StringUtils.isNotBlank(pasteQualityDTO.getScanFileReport())) {
            File deleteFile = FileUtils.getFile(FileManage.getQualityDir(currentSystemId), pasteQualityDTO.getScanFileReport());
            deleteFile.delete();
        }
    }


    private void pasteQualityFile(String copySystemId, String currentSystemId, QualityDTO copyQualityDTO) {
        try {
//             授权及告知
            if (StringUtils.isNotBlank(copyQualityDTO.getAuthFileQuality())) {
                File file = FileUtils.getFile(FileManage.getQualityDir(copySystemId), copyQualityDTO.getAuthFileQuality());
                File saveFile = FileUtils.getFile(FileManage.getQualityDir(currentSystemId), copyQualityDTO.getAuthFileQuality());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyQualityDTO.getNoticeFileQuality())) {
                File file = FileUtils.getFile(FileManage.getQualityDir(copySystemId), copyQualityDTO.getNoticeFileQuality());
                File saveFile = FileUtils.getFile(FileManage.getQualityDir(currentSystemId), copyQualityDTO.getNoticeFileQuality());
                FileManage.copyFile(file, saveFile);
            }
//            方案评审
            if (StringUtils.isNotBlank(copyQualityDTO.getReviewRecordFileScheme())) {
                File file = FileUtils.getFile(FileManage.getQualityDir(copySystemId), copyQualityDTO.getReviewRecordFileScheme());
                File saveFile = FileUtils.getFile(FileManage.getQualityDir(currentSystemId), copyQualityDTO.getReviewRecordFileScheme());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyQualityDTO.getConfirmRecordFileScheme())) {
                File file = FileUtils.getFile(FileManage.getQualityDir(copySystemId), copyQualityDTO.getConfirmRecordFileScheme());
                File saveFile = FileUtils.getFile(FileManage.getQualityDir(currentSystemId), copyQualityDTO.getConfirmRecordFileScheme());
                FileManage.copyFile(file, saveFile);
            }
//            报告评审
            if (StringUtils.isNotBlank(copyQualityDTO.getScanFileReport())) {
                File file = FileUtils.getFile(FileManage.getQualityDir(copySystemId), copyQualityDTO.getScanFileReport());
                File saveFile = FileUtils.getFile(FileManage.getQualityDir(currentSystemId), copyQualityDTO.getScanFileReport());
                FileManage.copyFile(file, saveFile);
            }
        } catch (IOException ex) {
            log.error("粘贴密评活动质量文件相关数据出错!", ex);
        }

    }


    private Result<String> pasteProofPerson(String copySystemId, String currentSystemId, String name) {
        var result = pasteQualification(copySystemId, currentSystemId);
        if (result.isSuccess()) {
            var optResult = pasteProve(copySystemId, currentSystemId);
            if (optResult.isSuccess()) {
                pasteProofSystemProcess(copySystemId, currentSystemId, name);
                return Result.success();
            } else {
                return Result.failed("");
            }
        } else {
            return Result.failed("");
        }
    }

    public Result<String> pasteQualification(String copySystemId, String currentSystemId) {
        List<EvaluatorQualificationDTO> copyData = proofService.findEvaluatorQualifications(copySystemId);
        List<EvaluatorQualificationDTO> currentData = proofService.findEvaluatorQualifications(currentSystemId);
        List<EvaluatorQualificationDTO> modifyData = new ArrayList<>();
        for (var item : copyData) {
            var source = currentData.stream()
                    .filter(st -> StringUtils.equals(st.getName(), item.getName()))
                    .findFirst().orElse(null);
            if (Objects.isNull(source)) {
                item.setId("");
                item.setSystemId(currentSystemId);
            } else {
                item.setId(source.getId());
                item.setSystemId(source.getSystemId());
            }
            modifyData.add(item);
        }
        proofService.batchSaveEvaluatorQualification(modifyData);
        return Result.success();
    }

    public Result<String> pasteProve(String copySystemId, String currentSystemId) {
        EvaluatorProveDTO copyEvaluatorProveDTO = this.proofService.findEvaluatorProve(copySystemId);
        EvaluatorProveDTO pasteEvaluatorProveDTO = this.proofService.findEvaluatorProve(currentSystemId);
        deleteProveFile(currentSystemId, pasteEvaluatorProveDTO);
        pasteProveFile(copySystemId, currentSystemId, copyEvaluatorProveDTO);
        proofService.saveEvaluatorProve(currentSystemId, copyEvaluatorProveDTO);
        return Result.success();
    }

    public void deleteProveFile(String currentSystemId, EvaluatorProveDTO pasteEvaluatorProveDTO) {
        if (StringUtils.isNotBlank(pasteEvaluatorProveDTO.getProveFile1())) {
            File deleteFile = FileUtils.getFile(FileManage.getProveDir(currentSystemId), pasteEvaluatorProveDTO.getProveFile1());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteEvaluatorProveDTO.getProveFile2())) {
            File deleteFile = FileUtils.getFile(FileManage.getProveDir(currentSystemId), pasteEvaluatorProveDTO.getProveFile2());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteEvaluatorProveDTO.getEditorProveFile())) {
            File deleteFile = FileUtils.getFile(FileManage.getProveDir(currentSystemId), pasteEvaluatorProveDTO.getEditorProveFile());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteEvaluatorProveDTO.getAuditProveFile())) {
            File deleteFile = FileUtils.getFile(FileManage.getProveDir(currentSystemId), pasteEvaluatorProveDTO.getAuditProveFile());
            deleteFile.delete();
        }
        if (StringUtils.isNotBlank(pasteEvaluatorProveDTO.getApproveProveFile())) {
            File deleteFile = FileUtils.getFile(FileManage.getProveDir(currentSystemId), pasteEvaluatorProveDTO.getApproveProveFile());
            deleteFile.delete();
        }
    }

    public void pasteProveFile(String copySystemId, String currentSystemId, EvaluatorProveDTO copyEvaluatorProveDTO) {
        try {
            if (StringUtils.isNotBlank(copyEvaluatorProveDTO.getProveFile1())) {
                File file = FileUtils.getFile(FileManage.getProveDir(copySystemId), copyEvaluatorProveDTO.getProveFile1());
                File saveFile = FileUtils.getFile(FileManage.getProveDir(currentSystemId), copyEvaluatorProveDTO.getProveFile1());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyEvaluatorProveDTO.getProveFile2())) {
                File file = FileUtils.getFile(FileManage.getProveDir(copySystemId), copyEvaluatorProveDTO.getProveFile2());
                File saveFile = FileUtils.getFile(FileManage.getProveDir(currentSystemId), copyEvaluatorProveDTO.getProveFile2());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyEvaluatorProveDTO.getEditorProveFile())) {
                File file = FileUtils.getFile(FileManage.getProveDir(copySystemId), copyEvaluatorProveDTO.getEditorProveFile());
                File saveFile = FileUtils.getFile(FileManage.getProveDir(currentSystemId), copyEvaluatorProveDTO.getEditorProveFile());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyEvaluatorProveDTO.getAuditProveFile())) {
                File file = FileUtils.getFile(FileManage.getProveDir(copySystemId), copyEvaluatorProveDTO.getAuditProveFile());
                File saveFile = FileUtils.getFile(FileManage.getProveDir(currentSystemId), copyEvaluatorProveDTO.getAuditProveFile());
                FileManage.copyFile(file, saveFile);
            }
            if (StringUtils.isNotBlank(copyEvaluatorProveDTO.getApproveProveFile())) {
                File file = FileUtils.getFile(FileManage.getProveDir(copySystemId), copyEvaluatorProveDTO.getApproveProveFile());
                File saveFile = FileUtils.getFile(FileManage.getProveDir(currentSystemId), copyEvaluatorProveDTO.getApproveProveFile());
                FileManage.copyFile(file, saveFile);
            }
        } catch (Exception ex) {
            log.error("粘贴密评人员资格证明相关数据出错!", ex);
        }
    }

    private Result<String> pasteProofGrading(String copySystemId, String currentSystemId, String name) {
        GradingDTO copyGradingDTO = proofService.findGrading(copySystemId);
        GradingDTO pasteGradingDTO = proofService.findGrading(currentSystemId);
        deleteGradingFile(currentSystemId, pasteGradingDTO);
        pasteGradingFile(copySystemId, currentSystemId, copyGradingDTO);
        var result = proofService.saveGrading(currentSystemId, copyGradingDTO);
        if (result.isSuccess()) {
            pasteProofSystemProcess(copySystemId, currentSystemId, name);
            return Result.success();
        } else {
            return Result.failed("");
        }
    }

    public void deleteGradingFile(String currentSystemId, GradingDTO pasteGradingDTO) {
        if (StringUtils.isNotBlank(pasteGradingDTO.getScanFile())) {
            File deleteFile = FileUtils.getFile(FileManage.getGradingDir(currentSystemId), pasteGradingDTO.getScanFile());
            deleteFile.delete();
        }
    }

    public void pasteGradingFile(String copySystemId, String currentSystemId, GradingDTO copyGradingDTO) {
        try {
            if (StringUtils.isNotBlank(copyGradingDTO.getScanFile())) {
                File file = FileUtils.getFile(FileManage.getGradingDir(copySystemId), copyGradingDTO.getScanFile());
                File saveFile = FileUtils.getFile(FileManage.getGradingDir(currentSystemId), copyGradingDTO.getScanFile());
                FileManage.copyFile(file, saveFile);
            }
        } catch (IOException ex) {
            log.error("粘贴系统定级匹配证明证明相关数据出错!", ex);
        }

    }

}